import sqlalchemy
from sqlalchemy import create_engine
import time
import datetime
import pandas as pd
import requests
from  tqdm import tqdm

TABLE_LISTED_OPEN_ENDED_FUND = "shenge_money_fund_listed_open_ended_fund"
TABLE_FUND_DAY_NET = "shenge_money_fund_day_net"
TABLE_FUND_DAY_K_DATA = "shenge_money_fund_day_k_data"




def connect_db():
    connect_info = 'mysql+pymysql://paqi:chs518518!@rm-bp1v9plylv4340as7qo.mysql.rds.aliyuncs.com:3306/paqi?charset=utf8'
    engine = create_engine(connect_info)
    return engine



def group_time_execute(tinme_duration,groups_size,datas,func,**kargs):
    #(endDate - startDate).seconds
    for i in range(len(datas)):
        duration = groups_size
        if i >0:
            i = i*duration
        if (i+duration) >= len(datas):
            groupcodes = datas[i::]
            if len(groupcodes)> 0:
                func(groupcodes,kargs)
                print("start sleep"+str(tinme_duration))
                time.sleep(tinme_duration)
            else:
                break

        else:
            groupcodes = datas[i:i+duration]
            func(groupcodes,kargs)
            print("start sleep" + str(tinme_duration))
            time.sleep(tinme_duration)





WAI_ZAO_MODULE_IN_FUND_DAY_KLINE_URL = "http://api.waizaowang.com/doc/getCnFundADayKLine"
WAI_ZAO_MODULE_IN_FUND_DAY_KLINE_URL_DEMO_PARAM = {
    "code":'159915',
    "startDate":'2021-12-09',
    "endDate":"2100-01-01",
    "fields":['code','name','tdate','open','close','high','low','ktype'],
    "export":"0",
    "ktype":"101",
    "token":"0"
}



WAI_ZAO_MODULE_IN_FUND_HOUR_KLINE_URL = "http://api.waizaowang.com/doc/getCnFundHourKLine"
WAI_ZAO_MODULE_IN_FUND_HOUR_KLINE_URL_DEMO_PARAM = {
    "code":'159915',
    "startDate":'2021-12-09',
    "endDate":"2100-01-01",
    "fields":"code,tdate,name",
    "export":"0",
    "token":"0"
}
WAI_ZAO_MODULE_IN_FUND_HOUR_KLINE_URL_DEMO_CODE =''
TOKEN = "082d308c05ba16252b74768f737a45d2"


def getFundCodes(conn):
    sql = "SELECT  * FROM %s " % TABLE_LISTED_OPEN_ENDED_FUND
    df = pd.read_sql(sql=sql, con=conn, index_col="id")
    fund_codes = list(df['fund_code'])
    return fund_codes

def getFundDailyData(fund_code,start_time,ktype,conn):
    param = WAI_ZAO_MODULE_IN_FUND_DAY_KLINE_URL_DEMO_PARAM.copy()
    param['fields'] = ",".join(param['fields'])
    param['startDate'] = start_time
    param['ktype'] = ktype
    param['token'] = TOKEN
    param['code'] = fund_code
    r1 = requests.get(url=WAI_ZAO_MODULE_IN_FUND_DAY_KLINE_URL, params=param)
    try:
        result = r1.text
        datas = result.split(';\n')
        dictDatas = []
        for data in datas:
            fields = data.split(",")
            dictData = {}
            i = 0
            for resparam in WAI_ZAO_MODULE_IN_FUND_DAY_KLINE_URL_DEMO_PARAM['fields']:
                dictData[resparam] = fields[i]
                i = i + 1
            dictDatas.append(dictData)

        df1 = pd.DataFrame(dictDatas)
        df1.to_sql(TABLE_FUND_DAY_K_DATA, con=conn,  if_exists="append",index=False)
        print("fund pull success:" + str(fund_code))
    except Exception as e:
        print("fund pull success:"+str(fund_code))



def getFundsDailyData(fund_codes,kargs):
    for fund_code in fund_codes:
        getFundDailyData(fund_code,kargs['startTime'],kargs['ktype'],kargs['conn'])








conn = connect_db()

import time
today = time.strftime("%Y-%m-%d")
print(today)
print("抓取")
group_time_execute(1,1000,getFundCodes(conn),getFundsDailyData,startTime='2021-12-24',ktype='101',conn =conn)






